package com.xz.courseschedule.ai;

import com.xz.courseschedule.common.dto.TimeTableClassDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: zys
 * @CreateTime: 2025-05-29
 */


@Service
public class ChatService {

    @Autowired
    private RagService ragService;

    @Autowired
    private OllamaService ollamaService;

    public Flux<String> getAiResponse(String userQuestion) {  // 修改返回类型为Flux
        // 1. 检索相关信息
        List<TimeTableClassDto> relevantFacts = ragService.searchRelevantInfo(userQuestion);

        // 2. 构造Prompt
        StringBuilder context = new StringBuilder();
        for (TimeTableClassDto fact : relevantFacts) {
            context.append("班级: ").append(fact.getClassName())
                    .append(", 课程: ").append(fact.getCourseName())
                    .append(", 教师: ").append(fact.getTeacherName())
                    .append(", 星期: ").append("星期"+((fact.getTimeslot()/5)+1) )
                    .append(", 学期: ").append(fact.getSemesterName())
                    .append("\n");
        }
        String prompt = "";
        if (userQuestion.contains("课")){
            prompt = "根据以下信息回答用户问题:\n\n当前日期是:" +  context.toString() + "\n用户问题: " + userQuestion + "\n直接列出，不需要额外说明";
        }else{
            prompt = userQuestion;
        }


        System.out.println(prompt);

        // 3. 调用模型并返回流
        return ollamaService.queryModel(prompt);  // 直接传递Ollama返回的流
    }
}
